using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using System.IO;

namespace GameOfLife
{
    public class GolGUI : Form
    {

        Button stepButton;
        Button randomButton;
        Button loadMatrixButton;
        ComboBox rulesetBox;

        GOLController controller;

        private int xboardsize;
        private int yboardsize;
        private int xstart;
        private int ystart;
        private int buttonbox_y;

        Pen boxpen = new Pen(Color.Black);
        SolidBrush boxbrush = new SolidBrush(Color.Red);

        public GolGUI(GOLController iController, string initialRuleset)
        {
            controller = iController;

            this.position();
            this.Name = "Game of Life Simulator";

            stepButton = new Button();
            stepButton.Text = "Step";
            stepButton.Location = new Point(20, buttonbox_y + 20);
            stepButton.Click += new EventHandler(StepButton_Click);
            this.Controls.Add(stepButton);
            

            randomButton = new Button();
            randomButton.Text = "Random";
            randomButton.Location = new Point(100, buttonbox_y + 20);
            randomButton.Click += new EventHandler(RandomButton_Click);
            this.Controls.Add(randomButton);

            
            rulesetBox = new ComboBox();
            foreach (string s in controller.rulesets.Keys)
            {
                rulesetBox.Items.Add(s);
            }
            rulesetBox.SelectedItem = initialRuleset;
            rulesetBox.Location = new Point(200, buttonbox_y + 20);
            rulesetBox.SelectedValueChanged += new EventHandler(rulesetBox_SelectedValueChanged);
            this.Controls.Add(rulesetBox);


            loadMatrixButton = new Button();
            loadMatrixButton.Text = "Load Matrix";
            loadMatrixButton.Location = new Point(500, buttonbox_y + 20);
            loadMatrixButton.Click += new EventHandler(loadMatrixButton_Click);
            this.Controls.Add(loadMatrixButton);
            

             
        }

        void rulesetBox_SelectedValueChanged(object sender, EventArgs e)
        {
            controller.OnRuleChange((string)rulesetBox.SelectedItem);
        }

        private void position()
        {
            xboardsize = controller.currentBoard.SizeX();
            yboardsize = controller.currentBoard.SizeY();

            int windowx;
            int windowy; 

            if (xboardsize >= 100)
            {
                xstart = 0;
                windowx = 6 * xboardsize + 10;
            }
            else
            {
                xstart = 300 - (3 * xboardsize);
                windowx = 600;
            }


            if (yboardsize >= 100)
            {
                ystart = 0;
                windowy = 6 * yboardsize + 80;
            }
            else
            {
                ystart = 300 - (3 * yboardsize);
                windowy = 680;
            }

            buttonbox_y = windowy - 80;
            this.Size = new Size(windowx, windowy);
        }

        public void drawSimulation()
        {
            this.Invalidate();
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (xboardsize != controller.currentBoard.SizeX() || yboardsize != controller.currentBoard.SizeY())
            {
                this.position();
            }
            
            for (int j = 0; j < yboardsize; j++)
            {
                for (int i = 0; i < xboardsize; i++)
                {
                    if (controller.currentBoard.GetCell(i, j))
                    {
                        e.Graphics.FillRectangle(boxbrush, i * 6 + xstart, j * 6 +ystart, 6, 6);
                    }
                    else
                    {
                        e.Graphics.DrawRectangle(boxpen, i * 6 +xstart, j * 6 +ystart, 6, 6);
                    }
                }
            }
             
        }

        private void StepButton_Click(object sender, EventArgs e)
        {
            controller.OnStep();
        }

        private void RandomButton_Click(object sender, EventArgs e)
        {
            controller.OnRandom();
        }



        private void loadMatrixButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                controller.OnLoadMatrix(openFileDialog1.FileName);
            }
        }


    }
}
